package net.sunzc.takephoto;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.SystemClock;
import android.util.Log;
import com.hanvon.faceRec.FaceCoreHelper;
import com.hanvon.faceRec.HWFaceLib;
import net.sunzc.hwfacetest.utils.FileUtils;
import net.sunzc.takephoto.utils.MyLog;
import net.sunzc.takephoto.utils.StringUtils;

import java.io.File;
import java.util.Arrays;

/**
 * Created by Administrator on 2017/1/17.
 */
public abstract class HWFace {
    public static final int TEMPLATE_SIZE = 5;
    private static final String FEATURE_DIR = "HanvonFeature", FEATURE_FILE = "hwFeature.dat";
    private static final int MAX_FACE_NUM = 1;
    private final String TAG = getClass().getSimpleName();
    protected ICamera.Size mCameraSize;
    Context mContext;
    ICamera mICamera;
    IFace mFaceListener;
    private File mRootDir, mFeatureDir, mFeatureFile;

    HWFace(Context context, ICamera camera) {
        this.mContext = context;
        this.mICamera = camera;
        mRootDir = context.getFilesDir();
        mFeatureDir = new File(mRootDir, FEATURE_DIR);
        if (!mFeatureDir.exists()) {
            mFeatureDir.mkdirs();
        }
        mFeatureFile = new File(mFeatureDir, FEATURE_FILE);
        if (mFeatureFile.exists()) mFeatureFile.delete();
        mCameraSize = camera.getPictureSize();
        MyLog.i(TAG, "相机尺寸" + mCameraSize);
    }

    public File getFeatureFile() {
        return mFeatureFile;
    }

    public void setFaceListener(IFace faceListener) {
        this.mFaceListener = faceListener;
    }

    public void exit(ICamera.ExitListener listener) {
        FaceCoreHelper.ReleaseFaceEngine();
        mICamera.exit(listener);
    }

    public void exit() {
        exit(new ICamera.ExitListener() {
            @Override
            public void onExit() {
                mFaceListener.onExit();
            }
        });
    }

    /**
     * 初始化并清空featureFile
     *
     * @return 返回初始化后的engine配置
     */
    protected byte[] initFeatureFile() {
        // 创建初始值
        byte[] data = new byte[16];
        byte[] value = StringUtils.Int2Bytes(0);
        System.arraycopy(value, 0, data, 0, 4);
        HWFaceLib.eDetectionDistanceRule nDisRule;
        nDisRule = HWFaceLib.eDetectionDistanceRule.Detect_Near;
        value = StringUtils.Int2Bytes(nDisRule.ordinal());
        System.arraycopy(value, 0, data, 4, 4);
        HWFaceLib.eDetectionStrictRule nStrRule;
        nStrRule = HWFaceLib.eDetectionStrictRule.Detect_Common;
        value = StringUtils.Int2Bytes(nStrRule.ordinal());
        System.arraycopy(value, 0, data, 8, 4);
        HWFaceLib.eRecogRule nRecRule;
        nRecRule = HWFaceLib.eRecogRule.Recog_Loose;
        value = StringUtils.Int2Bytes(nRecRule.ordinal());
        System.arraycopy(value, 0, data, 12, 4);
        // 写文件
        FileUtils.writeFile(getFeatureFile(), data, data.length);
        return data;
    }

    /**
     * 只检查画面中是否有人脸
     *
     * @param listener
     */
    public void checkFace(final FaceCheckListener listener) {
        SystemClock.sleep(1000);
        mICamera.takePhoto(new ICamera.PhotoListener() {
            @Override
            public void fetchPhoto(byte[] yuvImageData, Bitmap bitmap) {
//                yuvImageData = FileUtils.file2Byte(new File("/sdcard/recImage"));
                int[] detectNum = new int[1];
                int[] facePos = new int[12];
                if (FaceCoreHelper.FaceDetection(
                        yuvImageData,
                        mCameraSize.width,
                        mCameraSize.height,
                        facePos,
                        MAX_FACE_NUM,
                        detectNum)
                        == FaceCoreHelper.HW_OK) {
                    Log.i(TAG, "人脸检测:\n" +
                            "结果:" + true
                            + "rotateData" + yuvImageData.length +
                            "\nwidth:" + mCameraSize.width +
                            "\nheight:" + mCameraSize.height +
                            "\npnDetectFaceNum:" + detectNum[0] +
                            "\nfacePosition:" + Arrays.toString(facePos));
                    if (detectNum[0] > 0) {
                        listener.onCheckSuccess(bitmap, yuvImageData, facePos);
                        return;
                    }
                }
                Log.i(TAG, "人脸检测:\n" +
                        "结果:" + false
                        + "rotateData" + yuvImageData.length +
                        "\nwidth:" + mCameraSize.width +
                        "\nheight:" + mCameraSize.height +
                        "\npnDetectFaceNum:" + detectNum[0] +
                        "\nfacePosition:" + Arrays.toString(facePos));
                listener.onCheckFailure(bitmap);
            }
        });
    }

    public interface IFace {
        void onExit();
    }

    public interface FaceCheckListener {
        void onCheckSuccess(Bitmap bitmap, byte[] featureFromImage, int[] facePos);

        void onCheckFailure(Bitmap bitmap);
    }
}
